package com.ruoyi.client.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.client.pojo.form.BatchIdsForm;
import com.ruoyi.client.pojo.form.CartAddForm;
import com.ruoyi.client.pojo.form.CartUpdateForm;
import com.ruoyi.client.pojo.vo.CartVo;
import com.ruoyi.client.service.ClientCartService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Anonymous
@CrossOrigin
@RestController("ClientCartController")
@Api(value = "购物车模块", tags = "05.购物车操作")
@RequestMapping("/cart")
public class ClientCartController {
    @Resource
    private ClientCartService clientCartService;

    /**
     * 添加购物车（单个）
     */
    @SaCheckLogin
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "添加商品到购物车", notes = "商品ID和数量为必填，数量范围1-999")
    @PostMapping("/add")
    public R<Boolean> add(@RequestBody @Valid CartAddForm form) {
        boolean success = clientCartService.add(form);
        return success ? R.ok(true, "添加购物车成功") : R.fail(false, "添加购物车失败");
    }

    /**
     * 删除购物车（单个）
     */
    @SaCheckLogin
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "删除单个购物车项", notes = "传入购物车项ID即可删除")
    @ApiImplicitParam(name = "id", value = "购物车项ID", dataTypeClass = Long.class, required = true, example = "1")
    @DeleteMapping("/{id}")
    public R<Boolean> deleteById(@PathVariable Long id) {
        boolean success = clientCartService.deleteById(id);
        return success ? R.ok(true, "删除成功") : R.fail(false, "删除失败，购物车项不存在");
    }

    /**
     * 批量删除购物车项
     */
    @SaCheckLogin
    @ApiOperationSupport(order = 12)
    @ApiOperation(value = "批量删除购物车项", notes = "传入购物车项ID数组，支持批量删除")
    @DeleteMapping("/deleteBatch") // 修复：将POST改为DELETE，符合RESTful删除语义，避免路径冲突
    public R<Boolean> deleteBatch(@RequestBody @Valid BatchIdsForm form) {
        boolean success = clientCartService.deleteBatch(form);
        return success ? R.ok(true, "批量删除成功") : R.fail(false, "批量删除失败");
    }

    /**
     * 清空当前用户购物车
     */
    @SaCheckLogin
    @ApiOperationSupport(order = 13)
    @ApiOperation(value = "清空我的购物车", notes = "一键删除当前用户所有购物车项")
    @DeleteMapping("/clear")
    public R<Boolean> clearCart() {
        boolean success = clientCartService.deleteAllByMy();
        return success ? R.ok(true, "购物车清空成功") : R.fail(false, "购物车清空失败");
    }

    /**
     * 更新单个购物车项（数量/勾选状态）
     */
    @SaCheckLogin
    @ApiOperationSupport(order = 20)
    @ApiOperation(value = "更新购物车项", notes = "购物车项ID必填，数量（1-999）和勾选状态可选")
    @PutMapping("/update")
    public R<Boolean> updateCart(@RequestBody @Valid CartUpdateForm form) {
        boolean success = clientCartService.update(form);
        return success ? R.ok(true, "更新购物车项成功") : R.fail(false, "更新购物车项失败");
    }

    /**
     * 全选/全不选当前用户购物车项
     */
    @SaCheckLogin
    @ApiOperationSupport(order = 21)
    @ApiOperation(value = "购物车全选/全不选", notes = "1=全选，0=全不选，仅对当前用户生效")
    @ApiImplicitParam(name = "isChecked", value = "勾选状态（1=勾选，0=未勾选）", dataTypeClass = Integer.class, required = true, example = "1")
    @PutMapping("/checkAll/{isChecked}")
    public R<Boolean> checkAll(@PathVariable Integer isChecked) {
        boolean success = clientCartService.updateIsCheckedAll(isChecked);
        return success ? R.ok(true, "操作成功") : R.fail(false, "操作失败");
    }

    /**
     * 查询当前用户所有购物车项
     */
    @SaCheckLogin
    @ApiOperationSupport(order = 30)
    @ApiOperation(value = "查询我的购物车列表", notes = "返回当前用户所有购物车项，包含商品详情")
    @GetMapping("/listByMy")
    public R<List<CartVo>> listByMy() {
        List<CartVo> cartList = clientCartService.listByMy();
        return R.ok(cartList, "购物车列表查询成功");
    }

    /**
     * 查询当前用户已勾选的购物车项（用于结算）
     */
    @SaCheckLogin
    @ApiOperationSupport(order = 31)
    @ApiOperation(value = "查询已勾选购物车项", notes = "返回当前用户已勾选的购物车项，用于订单结算")
    @GetMapping("/listIsCheckedByMy")
    public R<List<CartVo>> listIsCheckedByMy() {
        List<CartVo> checkedList = clientCartService.listIsCheckedByMy();
        return R.ok(checkedList, "已勾选购物车项查询成功");
    }
}